#include "BfsVisitor.h"

void BfsFindPath::preInit()
{
	_edge_process_order.clear();
}

void BfsFindPath::initialize(const std::unique_ptr<IDigraph> &graph, int start_vertex)
{
	_previous.clear();
	_previous.resize(graph->getNumberOfVertices(), -1);
	_previous[start_vertex] = -2;
}

void BfsFindPath::comeToVertex(int vertex)
{

}

void BfsFindPath::processEdge(int from, int to)
{
	_edge_process_order.push_back(Edge(from, to));
	if (_previous[to] == -1)
		_previous[to] = from;
}

void BfsFindPath::leaveVertex(int vertex)
{

}

void BfsFindPath::finalize()
{
	_path.clear();
	if (_previous[_target_vertex] == -1) return;
	int vertex_on_path = _target_vertex;
	while (vertex_on_path != -2) _path.push_back(vertex_on_path), vertex_on_path = _previous[vertex_on_path];
	std::reverse(_path.begin(), _path.end());
}

std::vector<int> BfsFindPath::getResultedPath()
{
	return _path;
}

std::vector<Edge> BfsFindPath::getProcessOrder()
{
	return _edge_process_order;
}

void BfsFindPath::setTargetVertex(int vertex)
{
	_target_vertex = vertex;
}